package com.wukong.logisticsproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wukong.logisticsproject.ex.InsertException;
import com.wukong.logisticsproject.ex.UpdateException;
import com.wukong.logisticsproject.mapper.StaffMapper;
import com.wukong.logisticsproject.model.Staff;
import com.wukong.logisticsproject.model.SysPermission;
import com.wukong.logisticsproject.model.User;
import com.wukong.logisticsproject.model.Vehicle;
import com.wukong.logisticsproject.mapper.VehicleMapper;
import com.wukong.logisticsproject.service.IVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wukong.logisticsproject.utils.JqgridUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author wukong.com
 * @since 2020-10-08
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements IVehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private JqgridUtil jqgridUtil;

    /**
     * 获取车辆信息
     *
     * @return
     */
    @Override
    public List<Vehicle> getVehicleAllList() {
        System.out.println(vehicleMapper.selectList(null));
        return vehicleMapper.selectList(null);
    }


    /**
     * 车辆信息转JSON
     *
     * @param page
     * @param count
     * @return
     */
    @Override
    public String getAllVehicleJson(Integer page, Integer count) {
        List<Vehicle> vehicles = getVehicleAllList();
        int toIndex = count * page;
        if (vehicles.size() < toIndex) {
            toIndex = vehicles.size();
        }
        List<Vehicle> list = vehicles.subList(count * (page - 1), toIndex);
        return jqgridUtil.getJson(list, page + "", vehicles.size(), count);
    }

    /**
     * 查询空闲车辆
     *
     * @param branchSuoshu
     * @return
     */
    @Override
    public List<Vehicle> getVehicleByParamList(String branchSuoshu) {
        System.err.println("数据为3：" + branchSuoshu);
        QueryWrapper<Vehicle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vehicle_state", "空闲")
                .eq("branch_suoshu", branchSuoshu);
        List<Vehicle> vehicleList = vehicleMapper.selectList(queryWrapper);

        return vehicleList;
    }

    @Override
    public String getVehicleByParamJson(Integer page, Integer count, Staff staffInfo) {


        List<Vehicle> vehicles = getVehicleByParamList(staffInfo.getBranch());
        int toIndex = count * page;
        if (vehicles.size() < toIndex) {
            toIndex = vehicles.size();
        }
        List<Vehicle> list = vehicles.subList(count * (page - 1), toIndex);
        return jqgridUtil.getJson(list, page + "", vehicles.size(), count);
    }


    /**
     * 添加车辆信息
     *
     * @param vehicle
     * @return
     */
    @Override
    public Integer getInsertVehicle(Vehicle vehicle) {
        Integer rows = vehicleMapper.insertVehicle(vehicle);
        if (rows != 1) {
            throw new InsertException("插入车辆数据失败！！");
        }
        return rows;
    }

    /**
     * 删除车辆信息
     *
     * @param vehicleId
     * @return
     */
    @Override
    public Integer deleteVehicleById(Integer vehicleId) {
        System.out.println("vehicleId:" + vehicleId);
        return vehicleMapper.deleteById(vehicleId);
    }

    /**
     * 修改车辆信息
     *
     * @param vehicle
     * @return
     */
    @Override
    public Integer updateVehicle(Vehicle vehicle) {
        System.out.println(vehicle);
        Integer rows = vehicleMapper.updateById(vehicle);
        if (rows != 1) {
            throw new UpdateException("修改数据失败！！");
        }
        return rows;
    }

    /**
     * 查询JSON
     *
     * @param vehicleNum
     * @param vehicleDriver
     * @param vehicleState
     * @param branch
     * @param page
     * @param count
     * @return
     */
    @Override
    public String getVehicleJsonByParam(String vehicleNum, String vehicleDriver, String vehicleState, String branch, Integer page, Integer count) {
        List<Vehicle> Vehicle = getFindVehicleByParam(vehicleNum, vehicleDriver, vehicleState, branch);
        int toIndex = count * page;
        if (Vehicle.size() < toIndex) {
            toIndex = Vehicle.size();
        }
        List<Vehicle> list = Vehicle.subList(count * (page - 1), toIndex);
        return jqgridUtil.getJson(list, page + "", Vehicle.size(), count);
    }

    /**
     * 查询功能
     *
     * @param vehicleNum
     * @param vehicleDriver
     * @param vehicleState
     * @param branch
     * @return
     */
    @Override
    public List<Vehicle> getFindVehicleByParam(String vehicleNum, String vehicleDriver, String vehicleState, String branch) {
        System.err.println("!!!!!!!!!vehicleNum" + vehicleNum + ",vehicleDriver" + vehicleDriver + ",vehicleState" + vehicleState + ",branch" + branch);
        QueryWrapper<Vehicle> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(vehicleNum), "vehicle_num", vehicleNum)
                .like(!StringUtils.isEmpty(vehicleDriver), "vehicle_driver", vehicleDriver)
                .like(!StringUtils.isEmpty(vehicleState), "vehicle_state", vehicleState)
                .like(!StringUtils.isEmpty(branch), "branch_suoshu", branch);
        List<Vehicle> vehicleList = vehicleMapper.selectList(queryWrapper);
        return vehicleList;
    }

    /*
    修改车辆状态
     */
    @Override
    public Integer UpdateVehicleState(Vehicle vehicleInfo,String staffState) {
        QueryWrapper<Vehicle> vehicleQueryWrapper = new QueryWrapper<>();
        vehicleQueryWrapper.eq("vehicle_num", vehicleInfo.getVehicleNum());
        Vehicle vehicleOneInfo = vehicleMapper.selectOne(vehicleQueryWrapper);
        staffMapper.updateStaffStateById(vehicleOneInfo.getStaffId(),staffState);//修改转运员状态
        return vehicleMapper.update(vehicleInfo, vehicleQueryWrapper);
    }


}
